import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

/// 测试连接
Future<void> test() async {
  try {
    final client = MqttServerClient('10.0.0.85', 'clientId-test');
    client.port = 11883; // 默认端口（SSL通常为8883）
    client.autoReconnect = true; // 启用自动重连
    client.keepAlivePeriod = 20; // 心跳间隔，单位秒
    // client.logging(on: true); // 启用日志记录
    client.connectionMessage =
        MqttConnectMessage().authenticateAs("dev", "dev123456").startClean();
    client.onConnected = () {
      print('连接成功.....😀');
    };
    client.onDisconnected = () {
      print('连接断开.....😔');
    };
    client.onAutoReconnect = () {
      print('正在重新连接.....🙂');
    };
    client.onAutoReconnected = () {
      print('重新连接成功.....🙂');
    };
    await client.connect();
  } catch (e) {
    print('MQTT连接错误: $e');
  }
}

main() {
  test();
}
